package com.ak.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * @author bn
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {


    /**
     * 添加注解
     *
     * @param registry
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // 前端连接地方
        registry.addEndpoint("/myChat")
                .setAllowedOriginPatterns("*")
                .withSockJS();
    }


    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {

        // 点对点使用的订阅前缀（客户端订阅路径上会体现出来），不设置的话，默认也是/user/
        registry.setUserDestinationPrefix("/user");

        // 一个是群发，一个是 单发
        // 这里是使用 rabbitmq 连接
        registry.enableStompBrokerRelay("/exchange", "/topic", "/queue", "/amq/queue")
                .setRelayHost("47.100.104.187")
                .setRelayPort(61613)
                .setClientLogin("joker")
                .setClientPasscode("123456")
                .setVirtualHost("/chat");
        // rabbitmq 的虚拟机名称 chat
        // 登录密码 123456
        // host 如果在本机的话，一般为localhost,
        // 61613 为stomp 安装的端口,并不是rabbitmq 的端口,如果需要
        // 设置的统一前缀
        registry.setApplicationDestinationPrefixes("/app", "/api");

    }
}
